package com.tenement.service.impl;

import com.github.pagehelper.PageHelper;
import com.tenement.dao.TenementLogMapper;
import com.tenement.dao.TenementPerformanceMapper;
import com.tenement.domain.TenementLog;
import com.tenement.domain.TenementPerformance;
import com.tenement.domain.TenementUser;
import com.tenement.service.PerformanceService;
import com.tenement.utils.PerformanceTotal;
import com.tenement.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName : PerformanceServiceImpl  //类名
 * @Description : 业绩服务接口  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-07-12 20:48  //时间
 */
@Service
public class PerformanceServiceImpl implements PerformanceService {

    @Resource
    TenementPerformanceMapper performanceMapper;

    @Resource
    TenementLogMapper tenementLogMapper;

    /**
     * 业绩查询
     * @param performance
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<TenementPerformance> findAll(TenementPerformance performance, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return performanceMapper.findAll(performance);
    }

    /**
     * 总行数
     * @param performance
     * @return
     */
    @Override
    public Long overCount(TenementPerformance performance) {
        return performanceMapper.overCount(performance);
    }

    /**
     * 新增
     * @param performance
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int add(TenementPerformance performance, TenementUser user, String ip) {
        performance.setPerformanceId(UUID.randomUUID().toString().replaceAll("-", ""));
        performance.setRevision(1);
        performance.setCreatedBy(user.getUserName());
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogType(1);
        StringBuffer logBody= new StringBuffer();
        Tools.logPerformanceMod(performance,logBody);
        log.setLogBody(logBody.toString());
        log.setLogTitle("新增业绩");
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        tenementLogMapper.insertSelective(log);
        return performanceMapper.insertSelective(performance);
    }

    /**
     * 修改
     * @param performance
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int update(TenementPerformance performance, TenementUser user, String ip) {
        performance.setUpdatedBy(user.getUserName());
        performance.setUpdatedTime(new Date());
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogType(4);
        StringBuffer logBody= new StringBuffer();
        Tools.logPerformanceMod(performance,logBody);
        log.setLogBody(logBody.toString());
        log.setLogTitle("修改业绩");
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        tenementLogMapper.insertSelective(log);
        return performanceMapper.updateByPrimaryKeySelective(performance);
    }

    /**
     * id删除
     * @param id
     * @param ip
     * @param user
     * @return
     */
    @Override
    public int deleteById(String id, String ip, TenementUser user) {
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogType(2);
        StringBuffer logBody= new StringBuffer();
        Tools.logPerformanceMod(performanceMapper.selectByPrimaryKey(id),logBody);
        log.setLogBody(logBody.toString());
        log.setLogTitle("删除业绩");
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        tenementLogMapper.insertSelective(log);
        return performanceMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     * @param ids
     * @param ip
     * @param user
     * @return
     */
    @Override
    public int deleteAll(String[] ids, String ip, TenementUser user) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogType(2);
        StringBuffer logBody= new StringBuffer();
        Tools.logPerformanceMods(performanceMapper.selectAll(list),logBody);
        log.setLogBody(logBody.toString());
        log.setLogTitle("批量删除业绩");
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        tenementLogMapper.insertSelective(log);
        return performanceMapper.deleteAll(list);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @Override
    public TenementPerformance findById(String id) {
        return performanceMapper.selectByPrimaryKey(id);
    }

    /**
     * 数据总和
     * @param performance
     * @return
     */
    @Override
    public PerformanceTotal selectTotal(TenementPerformance performance) {
        return performanceMapper.selectTotal(performance);
    }
}
